﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS_Server;
using SMS_Server_Manager;

namespace SMS_Server_Manager.Forms
{
    public partial class ToHTTP : Form
    {
        public ToHTTP()
        {
            InitializeComponent();
            Method.SelectedIndex = 0;
        }

        private Action_Forward2HTTP toHttp;

        public Action_Forward2HTTP To_HTTP
        {
            get { return toHttp; }
            set
            {
                toHttp = value;
                FunctionName.Text = toHttp.ActionName;
                ParamFormat.Text = toHttp.ActionFormat;
                if (toHttp.Method == Action_Forward2HTTP.HttpMetod.GET)
                {
                    Method.SelectedIndex = 0;
                }
                else { Method.SelectedIndex = 1; }
                Url.Text = toHttp.URL;
                hasResponse.Checked = toHttp.HasResponse;
            }
        }

        private Form Sender;

        public System.Windows.Forms.DialogResult ViewDialog(Form sender)
        {
            Sender = sender;
            return this.ShowDialog(sender);
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            if (!validators.IsValidURL(Url.Text))
            {
                MessageBox.Show("A valid url is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //lets save the staticResponse now
            toHttp.URL = Url.Text;
            if (Method.SelectedIndex == 0) toHttp.Method = Action_Forward2HTTP.HttpMetod.GET; else toHttp.Method = Action_Forward2HTTP.HttpMetod.POST;
            toHttp.HasResponse = hasResponse.Checked;
            DataAccess DA = new DataAccess(Global.GetConfigurationDatabaseName(), Global.DatabasePassword);
            if (toHttp.actionID == 0)
            {
                DA.CommandString = "INSERT INTO Action_ToHTTP (FunctionName, Format, URL, Method, HasResponse) VALUES (@FunctionName, @Format, @URL, @Method, @HasResponse);";
                DA.AddParameter("@FunctionName", toHttp.ActionName);
                DA.AddParameter("@Format", toHttp.ActionFormat);
                DA.AddParameter("@URL", toHttp.URL);
                DA.AddParameter("@Method", toHttp.Method.ToString());
                DA.AddParameter("@HasResponse", toHttp.HasResponse);
                DA.ExecuteNonQuery();
                DA.CommandString = "SELECT ActionID FROM Action_ToHTTP WHERE FunctionName=@FunctionName AND Format=@Format AND URL=@URL AND Method=@Method AND HasResponse=@HasResponse;";
                DA.ExecuteReader();
                while (DA.Reader.Read())
                {
                    int ActionID = (int)DA.Reader.GetInt64(0);
                    bool ActionExist = false;
                    foreach (Incoming_Action ia in ((Function)((Mainform)Sender).FunctionList.SelectedItem).Actions)
                    {
                        if (ia.ActionID() == (long)ActionID && ia.ActionType() == ActionTypes.StaticResponse)
                        {
                            ActionExist = true;
                        }
                    }
                    if (!ActionExist)
                    {
                        toHttp.actionID = ActionID;
                        ((Function)((Mainform)Sender).FunctionList.SelectedItem).Actions.Add(toHttp);
                        break;
                    }
                }
                DA.Reader.Close();
                DA.Close();
                ((Mainform)Sender).ActionList.Items.Add(toHttp);
            }
            else
            {
                DA.CommandString = "UPDATE Action_ToHTTP SET URL=@URL, Method=@Method, HasResponse=@HasResponse WHERE ActionID=@ActionID;";
                DA.AddParameter("@URL", toHttp.URL);
                DA.AddParameter("@Method", toHttp.Method.ToString());
                DA.AddParameter("@HasResponse", toHttp.HasResponse);
                DA.AddParameter("@ActionID", toHttp.actionID);
                DA.ExecuteNonQuery();
                DA.Close();
            }
            Global.unsaved_changes = true;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}
